--Created by 30 Minutes--
--Smart Selective prop remover--

TOOL.Tab 		= "Fallen Menu"
TOOL.Category		= "Construction"
TOOL.Name		= "#Smart Remover"
TOOL.Command		= nil
TOOL.ConfigName		= ""

if (CLIENT) then
	language.Add( "Tool_SSPR_name", "Smart Selective Prop Remover" )
	language.Add( "Tool_SSPR_desc", "Click on an object to select it" )
	language.Add( "Tool_SSPR_0", "Right click deselects. Reload to remove selected props." )
	language.Add( "Undone_medics", "Undone Warmelon" )
end

local function CheckValid( ent1 )

	if (ent1:IsValid()) then return true end
	
end

local function RemoveEntity( ent )

	if (ent:IsValid()) then
		ent:Remove()
	end

end

local function DoRemoveEntity( Entity )

	if (!Entity) then return false end
	if (!Entity:IsValid()) then return false end
	if (Entity:IsPlayer()) then return false end

	// Nothing for the client to do here
	if ( CLIENT ) then return true end

	// Remove all constraints (this stops ropes from hanging around)
	constraint.RemoveAll( Entity )
	
	// Remove it properly in 1 second
	timer.Simple( 1, RemoveEntity, Entity )
	
	// Make it non solid
	Entity:SetNotSolid( true )
	Entity:SetMoveType( MOVETYPE_NONE )
	Entity:SetNoDraw( true )
	
	// Send Effect
	local ed = EffectData()
		ed:SetEntity( Entity )
	util.Effect( "entity_remove", ed, true, true )
	
	return true
end

/*---------------------------------------------------------
   Name:	LeftClick
   Desc:	Selects entities that will be deleted
---------------------------------------------------------*/  
function TOOL:LeftClick( trace )
deName="select"
number=0
number=number+1
	if (CheckValid(trace.Entity)) then
		trace.Entity:SetColor( 255, 0, 0, 255 )
		trace.Entity:SetName(deName .. number)

end		
end

/*---------------------------------------------------------
   Name:	RightClick
   Desc:	Deselects entity from deletion.
---------------------------------------------------------*/  
function TOOL:RightClick( trace )
		entName = trace.Entity:GetName()--will be used in later release
		trace.Entity:SetColor( 255, 255, 255, 255 )
		trace.Entity:SetName("deselected")
		
		
		
end

function TOOL:Reload( trace )

		delName="select";
		ID = 0;
		finish = true
		
			while (finish) do
				finish = true
				ID = ID+1;
				selEnt=delName..ID -- selname = select (number)
				
					for k,v in pairs( ents.FindByName(selEnt)) do --Scans all select(ints) and returns entity as variable v
						DoRemoveEntity(v)
					end
					
				if (ID == 500) then--set limit 500
					finish = false
				else return end
				
			end
	 end 


function TOOL.BuildCPanel( CPanel )
   
   CPanel:AddControl( "Header", { Text = "Lulza", Description = "Epic lulz" } )
  
   CPanel:AddControl( "Label", { Text = "Left click to select props for deletion"} )
   CPanel:AddControl( "Label", { Text = "Right click to deselect props"} )
   CPanel:AddControl( "Label", { Text = "Reload to remove all selected props!"} )
  
  
  end 